﻿using Pattern21迭代器模式.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pattern21迭代器模式.Models
{
    /// <summary>
    /// 具体的迭代器
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ConcreteIterator<T> : IIterator<T>
    {
        private readonly List<T> _list;
        private int _index;

        public ConcreteIterator(List<T> list)
        {
            _list = list;
        }

        public bool HasNext()
        {
            return _index < _list.Count;
        }

        public T Next()
        {
            T result = default(T);
            if(HasNext())
            {
                return _list[_index++];
            }

            return result;
        }
    }
}
